//
//  JKPersonalCenterViewController.swift
//  afanti
//
//  Created by albert on 16/4/15.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

class JKPersonalCenterViewController: UIViewController {
    
    let reuseId = "reuseIdentifier"
    
    var mid = UIView()
    
    //模型数据
    lazy var dataList:[JKPersonalCenterCellModel] = {
        let arr = JKPersonalCenterCellModel.dataList()
        return arr
    }()
    
    //懒加载scrollView
    lazy var scrollView:UIScrollView = {
        
        let scrollView = UIScrollView(frame: self.view.bounds)
        scrollView.delegate = self
        scrollView.alwaysBounceVertical = true
        self.view.addSubview(scrollView)
        
        return scrollView
    }()

    //懒加载imageView
    lazy var imageView:UIImageView = {
        
        let imageView = UIImageView()
        let image = UIImage(named: "aft_person_center_bg")
        let imageViewH = (image?.size.height)! * self.view.width / (image?.size.width)!
        imageView.frame = CGRect(x: 0, y: 0, width: self.view.width, height: imageViewH)
        imageView.image = image
        
        return imageView
    }()
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.hidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //初始化导航栏
        view.backgroundColor = JKGlobalBackGroundColor

        //添加imageView
        view.addSubview(imageView)

        /** 设置按钮和分享按钮 */
        setupSettingAndShareButton()
        
        /** 设置头像按钮 */
        setupUserIcon("aft_person_default_icon", userName: "未登录")
        
        /** 设置中间的工具条 */
        setupMiddleView(["错题本", "我问的题", "我答的题"])
        
        /** 底部的tableView */
        setupTableView()
 
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        
    }
    
    /** 设置中间的工具条 */
    func setupMiddleView(titles: [String]){
        let middleViewH: CGFloat = 55
        let middleView = UIView()
        middleView.backgroundColor = UIColor(colorLiteralRed: 100/255.0, green: 75/255.0, blue: 50/255.0, alpha: 0.5)
        let middleViewY = imageView.y + imageView.height - middleViewH
        middleView.frame = CGRect(x: 0, y: middleViewY, width: view.width, height: middleViewH)
        scrollView.addSubview(middleView)
        
        let buttonCount = titles.count
        let buttonW = view.width / CGFloat(buttonCount)
        
        //循环添加按钮
        for i in 0..<buttonCount{
            let button = UIButton(type: UIButtonType.Custom)
            button.adjustsImageWhenHighlighted = false
            button.height = middleViewH
            button.width = buttonW
            button.y = 0
            button.x = CGFloat(i) * (button.width)
            button.setTitle(titles[i], forState: UIControlState.Normal)
            button.tag = i
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            middleView.addSubview(button)
        }
        
        let separatorCount = buttonCount - 1
        
        //循环添加分割线
        for i in 0..<separatorCount{
            let separatorView = UIView()
            separatorView.backgroundColor = UIColor.whiteColor()
            separatorView.width = 1
            separatorView.height = middleViewH * 0.5
            separatorView.centerY = middleView.height * 0.5
            separatorView.x = buttonW + buttonW * CGFloat(i)
            middleView.addSubview(separatorView)
        }
        
        mid = middleView
    }
    
    /** 点击中间工具条的按钮 */
    func buttonClick(button: UIButton){
//        if button.tag == 0{
//            JKLoginTool.show(self)
//        }
        JKLoginTool.show(self)
    }
    
    /** 设置头像按钮 */
    func setupUserIcon(imageName: String, userName: String){
        //头像按钮
        let userIconBtn = JKUserIconButton(type: UIButtonType.Custom)
        
        //尺寸和位置
        userIconBtn.bounds = CGRect(x: 0, y: 0, width: 72, height: 72 + 25)
        let userIconBtnCenterY = imageView.y + imageView.height * 0.5
        userIconBtn.center = CGPoint(x: view.width * 0.5, y: userIconBtnCenterY)
        
        //图片和文字
        let userIconImage = UIImage.circleImage(imageName, borderColor: UIColor.whiteColor(), borderWidth: 5)
        userIconBtn.setImage(userIconImage, forState: UIControlState.Normal)
        userIconBtn.setTitle(userName, forState: UIControlState.Normal)
        userIconBtn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        //userIconBtn.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Normal)
        userIconBtn.titleLabel?.font = UIFont.systemFontOfSize(13)
        
        userIconBtn.addTarget(self, action: "userIconBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        scrollView.addSubview(userIconBtn)
    }
    
    //MARK: - 点击头像按钮
    func userIconBtnClick(){
        // 如果已经登录
        
        // 如果没有登录
        JKLoginTool.show(self)
    }
    
    /** 设置按钮和分享按钮 */
    func setupSettingAndShareButton(){
        //设置按钮
        let settingBtn = UIButton(type: UIButtonType.Custom)
        settingBtn.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
        settingBtn.center = CGPoint(x: view.width * 0.9, y: 60)
        settingBtn.setImage(UIImage(named: "aft_person_center_setting"), forState: UIControlState.Normal)
        //添加事件
        settingBtn.addTarget(self, action: "settingClick", forControlEvents: UIControlEvents.TouchUpInside)
        scrollView.addSubview(settingBtn)
        
        //分享按钮
        let shareBtn = UIButton(type: UIButtonType.Custom)
        shareBtn.bounds = settingBtn.bounds
        shareBtn.center = CGPoint(x: view.width * 0.1, y: 60)
        shareBtn.setImage(UIImage(named: "aft_person_center_share"), forState: UIControlState.Normal)
        //添加事件
        shareBtn.addTarget(self, action: "shareClick", forControlEvents: UIControlEvents.TouchUpInside)
        scrollView.addSubview(shareBtn)
    }
    
    /** 点击设置 */
    func settingClick(){
        
        let settingVc = JKSettingTableViewController.init()
        self.navigationController?.pushViewController(settingVc, animated: true)
    }
    
    /** 点击分享 */
    func shareClick(){
        print("分享")
    }
    
    /** tableView */
    func setupTableView(){
        
        let tableView = UITableView()

        tableView.width = view.width
        tableView.height = view.height - imageView.height - 49
        tableView.x = 0
        tableView.y = CGRectGetMaxY(mid.frame)
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 35)
        
        tableView.dataSource = self
        tableView.delegate = self
        
        scrollView.addSubview(tableView)
    }
}

extension JKPersonalCenterViewController: UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate{
    
    //MARK: - scrollView的代理方法
    internal func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //如果上划，就把背景图添加到scrollView，并置于底部
        if scrollView.contentOffset.y > 0{
            scrollView.addSubview(imageView)
            scrollView.insertSubview(imageView, atIndex: 0)
            
        }else{//如果下划，就把背景图添加到view，并置于底部
            view.addSubview(imageView)
            view.insertSubview(imageView, atIndex: 0)
        }
    
        //图片下划时的缩放比例
        var scale = ((-scrollView.contentOffset.y) * 2 + imageView.height) / imageView.height
        
        //只放大，不缩小
        scale = (scale >= 1) ? scale : 1
        
        //设置缩放
        self.imageView.transform = CGAffineTransformMakeScale(scale, scale)
    }
    
    //返回多少行
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //创建cell
        var cell = tableView.dequeueReusableCellWithIdentifier(reuseId)
        if cell == nil{
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseId)
        }
        
        //取出数据
        let model = dataList[indexPath.row]
        
        //给cell赋值
        cell?.textLabel?.text = model.title
        if indexPath.row == 3{
            cell?.detailTextLabel?.text = "0关注/0粉丝"
        }
        
        cell?.imageView?.image = UIImage(named: model.icon!)
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        
        //返回cell
        return cell!
    }
    
    //设置cell高度
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return tableView.height / CGFloat(dataList.count)
    }
}
